package market

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/market"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/market/outbound_crowd"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type OutboundCrowdController struct {
	controller.BaseController
	outboundCrowdService outbound_crowd.OutboundCrowdServiceIface
}

// NewOutboundCrowdController 外呼人群
func NewOutboundCrowdController(
	outboundCrowdService outbound_crowd.OutboundCrowdServiceIface,
) *OutboundCrowdController {
	return &OutboundCrowdController{
		outboundCrowdService: outboundCrowdService,
	}
}

func (c *OutboundCrowdController) RegisterRouter(g *gin.RouterGroup) {
	// 人群列表
	g.POST("crowd/get-list", c.GetCrowdList)
	// 人群详情
	g.POST("crowd/detail", c.GetCrowdDetail)
	// 添加、修改人群
	g.POST("crowd/add", c.AddCrowd)
	// 人群详情
	g.POST("crowd/delete", c.DeleteCrowd)
	// 获取中标人群列表
	g.POST("crowd/get-winbid-crowd", c.GetCrowdByWinbid)
	// 获取甲方单位人群列表
	g.POST("crowd/get-developer-crowd", c.GetCrowdByDeveloper)
	// 获取RPA人群列表
	g.POST("crowd/get-rpa-crowd", c.GetCrowdByRpa)
	// 人群详情：中标人群列表
	g.POST("crowd/detail-winbid", c.CrowdDetailWinbid)
	// 人群详情：甲方单位人群列表
	g.POST("crowd/detail-developer", c.CrowdDetailDeveloper)

}

func (c *OutboundCrowdController) GetCrowdList(ctx *gin.Context) {
	req := &dto.GetCrowdListRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.outboundCrowdService.GetCrowdList(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/get-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *OutboundCrowdController) GetCrowdDetail(ctx *gin.Context) {
	req := &dto.GetCrowdDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.outboundCrowdService.GetCrowdDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *OutboundCrowdController) AddCrowd(ctx *gin.Context) {
	req := &dto.AddCrowdRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	if req.Type != constants.CrowdTypeWinBid && req.Type != constants.CrowdTypeDeveloper && req.Type != constants.CrowdTypeRpa {
		stark.Logger.Errorf(ctx, "crowd/add err:%s", "type参数错误")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "type参数错误")
		return
	}

	res, err := c.outboundCrowdService.AddCrowd(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/add err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *OutboundCrowdController) DeleteCrowd(ctx *gin.Context) {
	req := &dto.DeleteCrowdRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.outboundCrowdService.DeleteCrowd(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/delete err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *OutboundCrowdController) GetCrowdByWinbid(ctx *gin.Context) {
	req := &dto.GetCrowdByWinbidRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.outboundCrowdService.GetCrowdByWinbid(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/get-winbid-crowd err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *OutboundCrowdController) GetCrowdByDeveloper(ctx *gin.Context) {
	req := &dto.GetCrowdByDeveloperRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.outboundCrowdService.GetCrowdByDeveloper(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/get-developer-crowd err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *OutboundCrowdController) GetCrowdByRpa(ctx *gin.Context) {
	req := &dto.GetCrowdByRpaRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.outboundCrowdService.GetCrowdByRpa(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/get-rpa-crowd err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *OutboundCrowdController) CrowdDetailWinbid(ctx *gin.Context) {
	req := &dto.CrowdDetailWinbidRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.outboundCrowdService.CrowdDetailWinbid(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/detail-winbid err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *OutboundCrowdController) CrowdDetailDeveloper(ctx *gin.Context) {
	req := &dto.CrowdDetailDeveloperRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.outboundCrowdService.CrowdDetailDeveloper(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "crowd/detail-developer err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}
